﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UwaySoft.EsheetServer.BizLogic;
using UwaySoft.EsheetServer.DataLayer;
using UwaySoft.EsheetServer.Help.Template;
using System.Xml;
using UwaySoft.EsheetServer.Help.Response;

namespace UwaySoft.EsheetServer.Help.WorkDAL
{
    public class HandleDAL : IWorkDAL
    {
        protected ISqlHelper SqlHelper
        {
            get { return Application.SqlHelper; }
        }

        public IResponse DoWork(string xmlString)
        {
            XmlDocument xmlDoc = null;
            try
            {
                xmlDoc = XmlHelper.ReadAsXmlDocument(xmlString);
            }
            catch (Exception exp)
            {
                Log.Debug(string.Format("撤销故障单/附加意见/催单 发送文字不符合XML格式，无法转换解析！\r\n{0}", xmlString));
                Log.Error(exp);

                return new HandleInfoResponse(false, string.Empty) { Remark = "处理意见 发送文字不符合XML格式，无法转换解析！" };
            }

            HandleModel followUpOperModel = null;
            try
            {
                followUpOperModel = new HandleModel(xmlDoc);
            }
            catch (Exception exp)
            {
                Log.Debug(string.Format("撤销故障单/附加意见/催单 无法解析文档\r\n{0}", xmlString));
                Log.Error(exp);
                return new HandleInfoResponse(false, string.Empty) { Remark = "处理意见 无法解析文档" };
            }
            return AddDemoWorkOrder(followUpOperModel);
        }

        private IResponse AddDemoWorkOrder(HandleModel handleModel)
        {
            try
            {
                string complaintsId = SqlHelper.ExecuteScalar<string>("SELECT DISTINCT COMPLAINTS_ID FROM MOD_COMPLAINTS_ITEM WHERE SERIALNO=:SERIALNO",
                                                                      SqlHelper.CreateParameter(":SERIALNO", handleModel.SerialNo));
                if(string.IsNullOrEmpty(complaintsId))
                    return new HandleInfoResponse(false, handleModel.SerialInfoNo) { Remark = "处理意见 未找到关联工单" };

                SqlHelper.ExecuteNonQuery("INSERT INTO MOD_COMPLAINTS_ITEM_LIST (COMPLAINTS_ID,SERIALNO,TYPE,REMARK,CURNODE,HANDLESTAFF,HANDLEDATE,SERIALINFONO) VALUES (:COMPLAINTS_ID,:SERIALNO,5,:REMARK,:CURNODE,:HANDLESTAFF,:HANDLEDATE,:SERIALINFONO)",
                    SqlHelper.CreateParameter(":COMPLAINTS_ID", complaintsId),
                    SqlHelper.CreateParameter(":SERIALNO", handleModel.SerialNo),
                    SqlHelper.CreateParameter(":REMARK", handleModel.HandleContent),
                    SqlHelper.CreateParameter(":CURNODE", handleModel.CurNode),
                    SqlHelper.CreateParameter(":HANDLESTAFF", handleModel.HandleStaff),
                    SqlHelper.CreateParameter(":HANDLEDATE", handleModel.HandleDate),
                    SqlHelper.CreateParameter(":SERIALINFONO", handleModel.SerialInfoNo));

                return new HandleInfoResponse(true, handleModel.SerialInfoNo) { Remark = "处理意见 接收成功" };
            }
            catch
            {
                return new HandleInfoResponse(false, handleModel.SerialInfoNo) { Remark = "处理意见 接收失败" };
            }

            
        }

        private object CheckedDbValue(object value)
        {
            switch (value.GetType().FullName)
            {
                case "System.String":
                    return string.IsNullOrEmpty(value.ToString()) ? DBNull.Value : value;
                default:
                    return DBNull.Value;
            }
        }
    }
}
